package com.gitee.zycra.future.job.client.socket;

import com.gitee.zycra.future.job.client.executor.CPUPercentExecutor;
import com.gitee.zycra.future.job.client.executor.MemoryPercentExecutor;
import com.gitee.zycra.future.job.client.executor.SyncStatusExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zycra
 */
@Slf4j
@Component
public class SocketClient {

    @Value("${job.admin.host}")
    private String host;

    @Value("${spring.application.name}")
    private String applicationName;

    private final ScheduledThreadPoolExecutor syncExecutor = new ScheduledThreadPoolExecutor(3);

    @PostConstruct
    public void init() {
        syncExecutor.scheduleAtFixedRate(new CPUPercentExecutor(), 1L, 1L, TimeUnit.SECONDS);
        syncExecutor.scheduleAtFixedRate(new MemoryPercentExecutor(), 1L, 1L, TimeUnit.SECONDS);
        syncExecutor.scheduleAtFixedRate(new SyncStatusExecutor(host, applicationName), 0L, 1L, TimeUnit.SECONDS);
    }

}
